open Castling;;
open Test_tk_token;;

module CalcToken = struct
	type t = token;;
	let eof: t = Eof;;
	let invalid: t option = None;;
	let compare = Pervasives.compare;;
	let pp ~(quali: bool) ~(paren: bool) (f: Format.formatter) (e: t): unit = (
		begin match e with
		| Number n ->
			if paren then Format.pp_print_string f "(";
			if quali then Format.pp_print_string f "Test_tk_token.";
			Format.pp_print_string f "Number ";
			Format.pp_print_int f n;
			if paren then Format.pp_print_string f ")"
		| Add ->
			if quali then Format.pp_print_string f "Test_tk_token.";
			Format.pp_print_string f "Add"
		| Sub ->
			if quali then Format.pp_print_string f "Test_tk_token.";
			Format.pp_print_string f "Sub"
		| Mul ->
			if quali then Format.pp_print_string f "Test_tk_token.";
			Format.pp_print_string f "Mul"
		| Div ->
			if quali then Format.pp_print_string f "Test_tk_token.";
			Format.pp_print_string f "Div"
		| Eof ->
			if quali then Format.pp_print_string f "Test_tk_token.";
			Format.pp_print_string f "Eof"
		| Error s ->
			if quali then Format.pp_print_string f "Test_tk_token.";
			Format.pp_print_string f "Error \"";
			Format.pp_print_string f (String.escaped s);
			Format.pp_print_char f '\"';
		end
	);;
	let pp_pattern ~(quali: bool) ~(paren: bool) (f: Format.formatter) (e: t): unit = (
		begin match e with
		| Number _ ->
			if paren then Format.pp_print_string f "(";
			if quali then Format.pp_print_string f "Test_tk_token.";
			Format.pp_print_string f "Number _";
			if paren then Format.pp_print_string f ")"
		| _ ->
			pp ~quali ~paren f e
		end
	);;
	let pp_typename ~(quali: bool) ~(paren: bool) (f: Format.formatter) (_: t list): unit = (
		if quali then Format.pp_print_string f "Test_tk_token.";
		Format.pp_print_string f "token"
	);;
end;;

module CalcPG = ParserGenerator (CalcToken);;
open CalcPG;;

let rec expr = lazy !! (
	one_of ~name:"expr" [
		"identity_term", term;
		"makeadd", expr >> skip (element Add) >> term;
		"makesub", expr >> skip (element Sub) >> term
	]
) and term = lazy !! (
	one_of ~name:"term" [
		"identity_number", element (Number 0);
		"makemul", term >> skip (element Mul) >> element (Number 0);
		"makediv", term >> skip (element Div) >> element (Number 0)
	]
);;

debug := true;;
generate ~ast_module:"calc" ~parser_module:"calc" expr;;
